<? 
	if(isset($_POST['btndelete']))
	{
		require("required/dbconnect.php");
		$deleteIds="";
				
		if(!empty($_POST['pro_id']))
		{
			$postCount = count($_POST['pro_id']);
			$msg="";
			
		
			if($postCount!=0)
			{
				for ( $i=0; $i < $postCount; $i++ ) 
				{
					unset($_SESSION['cart'][$_POST['pro_id'][$i]]);
				}
				
			}
			@sort($_SESSION['cart']);
		}
		header("Location:os_mycart.php");
	}
	
	if(isset($_POST['btnupdate']))
	{
		require("required/dbconnect.php");
		
		
		$no=0;
		foreach($_SESSION['cart'] as $cartItems)
		{
			$name="q_".$cartItems['id'];
			$quantity=$_POST[$name];
			if($quantity<0 || $quantity==0 || !is_numeric($quantity))
			{
				header("Location:os_mycart.php?error=1");
				die();
			}
			else
			{
				$itemId=$_SESSION['cart'][$no]['id'];
				$chk=mysql_query("select * from os_product_inventory where id=$itemId");
				
				if(mysql_result($chk,0,"quantity")<$quantity){
					header("Location:os_mycart.php?error=4&pid=".$_SESSION['cart'][$no]['id']);
					die();
				}
				else
				{
					$_SESSION['cart'][$no]['quantity']=$quantity;
				}
			}
			$no++;
		}
		header("Location:os_mycart.php?error=2");
	}
	
	$title="My cart";
	require("required/dbconnect.php");
	require("required/share_function.php");
	$_SESSION['pid']=-1;
	require("required/header.php");
	
	
	
	$_SESSION['billing']=1;
	

?>
<SCRIPT LANGUAGE="JavaScript">
		function checkall(form, prefix, checkall) 
		{
			var checkall = checkall ? checkall : 'chkall';
			for(var i = 0; i < form.elements.length; i++) {
				var e = form.elements[i];
				if(e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
					e.checked = form.elements[checkall].checked;
				}
			}
		}
		
		function checkfields(frm)
		{
		if(confirm("Confirm to delete?"))
		return true;
		else
		return false;
		}
		//  End -->
	</script>
			
			
				<table width="100%" cellpadding="5" cellspacing="0">
					  <td align="left" width="100%" class="page_title_bar">
                        My shopping cart							
                        </td>
					    <?
						if($header_total_items==0)
						{
					?>
					<tr>
						<td colspan="3" align="center" height="400" valign="top">
						<font color="#FF0000">Your shopping cart is empty</font><br>
						<a href="os_products.php">Start shopping now!</a>
						</td>
					</tr>
					<?
						}
						else
						{
					?>
					<tr>
						<Td height="400" valign="top"><br>
							<table width="100%">
							<?
									if(!empty($_GET['error']))
									{	
										if($_GET['error']==1)
										{
												
							?>
								<div class="dw_failed">Invalid quantity value!</div>
							<?
										}
										elseif($_GET['error']==2)
										{
							?>
								<div class="dw_success"><font color="#00CC00">Quantity updated!</font></div>
							<?				
										}
										elseif($_GET['error']==3)
										{
							?>
								<div class="dw_failed">This product is already in your cart</div>
							<?				
										}
										elseif($_GET['error']==4)
										{
											$pid=$_GET['pid'];
											
											$get_invalid=mysql_query("select a.description,a.quantity,b.product_name_eng from os_product_inventory a inner join os_products b on a.pid = b.id where a.id=$pid")or die(mysql_error());
											$invalid=mysql_fetch_array($get_invalid,MYSQL_ASSOC);
							?>
								<div class="dw_failed">The quantity for <strong><?= $invalid['product_name_eng'] ?>( <?= $invalid['description'] ?> )</strong> is not available</div>
							<?				
										}
											
									}
							?>
							
							<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm">
							<tr>
								<td>
                                <table width="90%" bgcolor="#CCCCCC" cellpadding="1" cellspacing="1" align="center">
								<tr class="table_header" align="center">
									<td width="4%" ><input class="checkbox" type="checkbox" name="chkall" onclick="checkall(this.form)"></td>
                                    <td width="42%" >Product</td><td width="15%">Product code</td>
                                    <td width="13%" >Unit price(<?= $_SESSION['currency_code'] ?>)</td><td width="11%">Quantity</td><td width="15%">Total price( <?= $_SESSION['currency_code'] ?>)</td>
								</tr>
							<?
									$total_item=0;
									$total_piece=0;
									$total_allprice=0;
									$no=0;
									
									foreach($_SESSION['cart'] as $cartItems)
									{
										$itemId=$cartItems['id'];
										$quantity=$cartItems['quantity'];
										$addon_ids=$cartItems['addon_id'];
										
										$get_items=mysql_query("select * from os_product_inventory where id=".$itemId);
										$item=mysql_fetch_array($get_items,MYSQL_ASSOC);
										$proId=$item['pid'];
										
										$get_product=mysql_query("select * from os_products where id=$proId");
										
										$a=mysql_fetch_array($get_product,MYSQL_ASSOC);	
										
										$get_pic=mysql_query("select * from os_productpic where product_id=".$a['id']." order by order_id asc limit 1");
					
										if(mysql_num_rows($get_pic)==0)
										{
											$pic="empty";
											$piclink="noimage.jpg";
											$fix="";
										}
										else
										{
											$pic=mysql_fetch_array($get_pic,MYSQL_ASSOC);
											
											$piclink=$pic['name'];
											
											if($pic["width"]-$pic["height"]>0)
											{
												$fix="width=155";
											}
											else
											{
												$fix="height=130";
											}
										}	
										
										$total_price=0;
										
										if($a['promotion']==1)
										{
											$total_price=$item['promotion_price']*$quantity;		
											$unit_price=$item['promotion_price'];
										}
										else
										{
											$total_price=$item['unit_price']*$quantity;
											$unit_price=$item['unit_price'];
										}

										$total_allprice=$total_allprice+$total_price;
										
										if($no%2==0)
										{
											$class1="table_item_even";
											$class2="table_item_odd";
										}
										else
										{
											$class1="table_item_odd";
											$class2="table_item_even";
										}
										
										
																				
							?>
								 <tr height="30" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" onmouseout="this.className='<?= $class1 ?>'" align="center">
									<td>
										<input type="checkbox" name="pro_id[]" value="<?= $no ?>">
									</td><em></em>
									<td>
										<table width="80%" cellpadding="5" align="center">
											<Td width="12%">
												<a href="<?= getProductUrl($a['id'],$a['clean_url'],$module_url_rewrite) ?> "><img border=0 src="productpic/thumbnails/<?= $piclink ?>" <?= $fix ?>></a>
											</Td>
											<td width="88%" valign="top">
												<a href="<?= getProductUrl($a['id'],$a['clean_url'],$module_url_rewrite) ?>"><?= $a['product_name_eng'] ?></a><br>
												<?= $item['description'] ?>
											</td>
										</table>
									</td>
									<td><?= $a['product_code'] ?></td>
									<td><?= number_format($_SESSION['currency_rate']*$unit_price,2) ?></td>
									<td><input type="text" value="<?= $quantity ?>" size="2" class="text" maxlength="3" name="q_<?= $itemId ?>"></td>
									<td align="right"><?= number_format($_SESSION['currency_rate']*$total_price,2) ?></td>
								</tr>
                            <?
								if(is_array($addon_ids))
								{
							?>
                            <tr height="30" valign="middle" class="table_addon_item"  onmouseover="this.className='table_item_mouseover'" onmouseout="this.className='table_addon_item'" >
                                  <td colspan="6">
                                      Add-on(s)
                                  </td>									
							</tr>
                            <?		
							
									for($addon=0;$addon<count($addon_ids);$addon++)
									{
										$addon_id=$addon_ids[$addon];
										$get_addon=mysql_query("select * from os_products where id='".$addon_id."'");	
										$ap=mysql_fetch_array($get_addon,MYSQL_ASSOC);
										
										$get_addon_items=mysql_query("select * from os_product_inventory where pid='".$addon_id."' order by id asc limit 1")or die(mysql_error());
										
										$addon_item=mysql_fetch_array($get_addon_items,MYSQL_ASSOC);
										
										$get_pic=mysql_query("select * from os_productpic where product_id='".$addon_id."' order by order_id asc limit 1")or die(mysql_error());
					
										if(mysql_num_rows($get_pic)==0)
										{
											$pic="empty";
											$piclink="noimage.jpg";
											$fix="";
										}
										else
										{
											$pic=mysql_fetch_array($get_pic,MYSQL_ASSOC);
											
											$piclink=$pic['name'];
											
											if($pic["width"]-$pic["height"]>0)
											{
												$fix="width=155";
											}
											else
											{
												$fix="height=130";
											}
										}	
										
										$unit_price=$addon_item['unit_price'];
										$total_price=$quantity*$unit_price;
										
										$total_allprice=$total_allprice+$total_price;
							?>
                            	<tr height="30" valign="top" class="table_addon_item"  onmouseover="this.className='table_item_mouseover'" onmouseout="this.className='table_addon_item'" align="center">
									<td>
										
									</td>
									<td>
										<table width="80%" cellpadding="5" align="center">
											<Td width="12%">
												<a href="<?= getProductUrl($ap['id'],$ap['clean_url'],$module_url_rewrite) ?> "><img border=0 src="productpic/thumbnails/<?= $piclink ?>" <?= $fix ?>></a>
											</Td>
											<td width="88%" valign="top">
												<a href="<?= getProductUrl($ap['id'],$ap['clean_url'],$module_url_rewrite) ?>"><?= $ap['product_name_eng'] ?></a><br>
												<?= $addon_item['description'] ?>
											</td>
										</table>
									</td>
									<td><?= $ap['product_code'] ?></td>
									<td><?= number_format($_SESSION['currency_rate']*$unit_price,2) ?></td>
									<td><?= $quantity ?></td>
									<td align="right"><?= number_format($_SESSION['currency_rate']*$total_price,2) ?></td>
								</tr>
                            
                            <?
									}
								}
							?>   
							<? 
								$total_item++;
								$total_piece=$total_piece+$quantity;
								$no++;	
									} 
							?>
							<tr height="30" class="table_footer">
								<td colspan="4"  align="center" >
								Your cart contains <?= $total_item ?> product(s), <?= $total_piece ?> item(s) </td>
								<? 
									$_SESSION['total_items']=$total_piece; 
									$_SESSION['total_overall_price']=$total_allprice;
								?>
								<td align="center">Total <?= $_SESSION['currency_code'] ?></td><td align="right"><font color="#FF0000"><strong><?= number_format($_SESSION['currency_rate']*$total_allprice,2) ?></strong></font> </td>
							</tr>					
							</table></td>
							</tr>
							<tr>
							<td colspan="6" align="center"><br>
							
							<input type="submit" value="Delete" class="button" name="btndelete"  onclick="return checkfields(this);"> 
							<input type="submit" value="Update price" name="btnupdate" class="button">
							<input type="button" value="Continue Shopping" class="button" onClick="window.open('os_products.php','_self')">
							<input type="button" value="Checkout" class="button" onClick="window.open('os_bill.php','_self')">
							</td>
							</tr>
							<tr>
								<td style="padding-left:30px"><br>
								- You may continue shopping<br>
								- You can have the product(s) in the cart removed by selecting the line item and clicking on "<strong>Delete</strong>"<br>
								- You can change the quantity of the item(s) that you have in the cart by clicking on "<strong>Update price</strong>"<br>
								- If you are done, you may proceed to the next step by clicking on "<strong>Checkout</strong>"<br></td>
							</tr>
							
							</form>
							
							
							</table>
							
							
					  </Td>
					</tr>	
					<? } ?>	  
			  	</table>
			
<? require("required/footer.php"); ?>	